package cn.com.jslh.boarpro.util;

import android.graphics.Color;
import android.text.InputType;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：周洲 on 2017/8/1 09:27
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class JSONUtil {
    public static List<JSONObject> JsonArray2List(JSONArray jsonArray) {
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                list.add(jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static String getStr(String key, JSONObject jsonObject) {
        try {
            if (jsonObject.has(key) && !jsonObject.getString(key).equals("null")) {
                return jsonObject.getString(key);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getColor(String key, JSONObject jsonObject) {
        String colorStr = JSONUtil.getStr(key, jsonObject);
        if (colorStr != null && colorStr.contains("#") && colorStr.length() == 7) {
            return Color.parseColor(colorStr);
        }
        return Color.BLACK; //默认黑色
    }

    public static int getInt(String key, JSONObject jsonObject) {
        try {
            if (jsonObject.has(key) && jsonObject.getInt(key) != -1)
                return jsonObject.getInt(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 10;
    }

    public static void configEditText(EditText editText, JSONObject config) {
        editText.setHintTextColor(JSONUtil.getColor("placeTextColor", config));
//        editText.setTextColor(JSONUtil.getColor("valueColor", config));
//        editText.setBackgroundColor(JSONUtil.getColor("valueBgColor", config));
        editText.setTextSize(JSONUtil.getInt("valueSize", config));
        int inputType = 0;
        try {
            inputType = config.getInt("inputType");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        switch (inputType) {
            case 1:
                break;
            case 2://整数
                editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 3: //小数
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            default:
                break;
        }
    }

    public static void configValueTextView(TextView textView, JSONObject config) {
//        textView.setHintTextColor(JSONUtil.getColor("placeTextColor", config));
        textView.setTextColor(JSONUtil.getColor("valueColor", config));
        textView.setBackgroundColor(JSONUtil.getColor("valueBgColor", config));
        textView.setTextSize(JSONUtil.getInt("valueSize", config));
//                                editText.setInputType(JSONUtil.getInt("inputType", config));
//        editText.setInputType(InputType.TYPE_CLASS_TEXT);
    }

    public static void configTextView(TextView textView, JSONObject config) {
        textView.setTextColor(JSONUtil.getColor("titleColor", config));
        textView.setTextSize(JSONUtil.getInt("titleSize", config));
        textView.setWidth(JSONUtil.getInt("titleWidth", config));
    }
}
